﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once
#include "GcmpGuiQt.h"
#include "IActionCoordinateInputWidget.h"
#include "WeakPtr.h"

#include <QDialog>
#include <vector>

namespace gcmp
{
    class CoordinateInputDefinition;
    class CoordinateInputEditor;
}

class QBoxLayout;
class QLineEdit;
class QLabel;


namespace gcmp
{
    /// \brief ActionCoordinateInputWidget是Action坐标输入控件
    class GCMP_GUI_QT_EXPORT ActionCoordinateInputWidget : public QDialog, public gcmp::IActionCoordinateInputWidget
    {
        Q_OBJECT

    public:
        ActionCoordinateInputWidget(QWidget *parent = 0);
        virtual ~ActionCoordinateInputWidget();

        virtual void Refresh() override;

        virtual void Show() override;
        virtual void Hide() override;

        virtual void HideAndClear() override;

        virtual bool IsUserCustomInput() const override { return m_userCustomInput; }

        virtual bool IsInInputState() const override;

    public:
        void InitWidget(std::vector<gcmp::OwnerPtr<gcmp::ICoordinateInputDefinition>>&& dlgCtrlDefines,
            std::function<bool(const std::wstring &editValue, double &coorDinate, std::wstring &errorTip)> &editCallBack, bool userCustomInput = false);

        void SetWindowTitle(const std::wstring& title);

    protected:
        // 唤醒坐标输入，获得输入焦点
        bool WakeOn(const std::wstring& keyStr);

        void ActiveWindow()const;

        void keyPressEvent(QKeyEvent *pEvent) override;

        // 设置焦点到视图窗口
        void SetFocusToCurrentView();

        CoordinateInputDefinition* GetDefaultEdit();
        CoordinateInputDefinition* SwitchToNextDefaultEdit();

        private slots:
        void OnEditFinished();

    private:
        //保存/恢复窗口状态
        void ClearControl();
        bool eventFilter(QObject *object, QEvent *event);

        bool CheckEditInputValidate(); // 检查输入合法性

        QBoxLayout* GetLayout() const { return m_pBoxLayout; }
        void SetLayout(QBoxLayout* pLayout);

    private:
        QBoxLayout*                 m_pBoxLayout;   // 坐标窗口控件的布局
        gcmp::OwnerPtr<QLabel>      m_opTitleLabel; // 提示信息
        std::vector<gcmp::OwnerPtr<gcmp::CoordinateInputEditor>>   m_controls;
        std::vector<WeakPtr<CoordinateInputDefinition>>            m_controlDefines;
        WeakPtr<CoordinateInputDefinition>                         m_pDefaultEdit;
        QLineEdit*                                                 m_pCurLineEdit;

        typedef std::function<bool(const std::wstring &editValue, double &coorDinate, std::wstring &errorTip)> EditCallback;
        EditCallback m_editCallBack;
        bool         m_userCustomInput;
        bool         m_isDeleteLater;
    };
}
